package com.tarim.streetdance.service.impl;

import com.tarim.streetdance.mapper.ArticleMapper;
import com.tarim.streetdance.model.Article;
import com.tarim.streetdance.service.ArticleService;
import com.tarim.streetdance.vo.Criteria;
import com.tarim.streetdance.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    ArticleMapper articleMapper;

    @Override
    public int insertSelective(Article record) {
        record.setViewsnum(0L);
        record.setCreateTime(new Date());
        record.setDr(0);
        return articleMapper.insertSelective(record);
    }

    @Override
    public int updateByPrimaryKeySelective(Article record) {
        record.setModifyTime(new Date());
        return articleMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public PageBean<Article> selectByCondition(Criteria criteria, PageBean<Article> pageBean) {
        criteria.addExtField("pageIndex", pageBean.getPageIndex() * pageBean.getPageSize());
        criteria.addExtField("pageSize", pageBean.getPageSize());
        List<Article> list = articleMapper.selectByCondition(criteria);
        Long count = articleMapper.selectByConditionCount(criteria);
        pageBean.setTotalCount(count);
        pageBean.setData(list.toArray());
        return pageBean;
    }
}